import express from 'express'
var createError = require('http-errors');
import { NextFunction, Request, Response } from 'express'
// import cookieParser = require('body-parser');
const path = require('path');
import cors from 'cors'
import userRouter from "./router/user"
import videoRouter from "./router/video"
import remarkRouter from "./router/remark"
import session from "express-session"
import redisClient from "./db/connectRedis"
import loginCheck from "./loginCheck/loginCheck"
const RedisStore = require("connect-redis")(session)
const sessionStore = new RedisStore({
  client:redisClient.redisClient
})
class App {
  public app: express.Application;
  constructor() {
    this.app = express()
    this.config()
    this.app.use("/api/user", userRouter)
    // this.loginCh()
    this.app.use("/api/video",videoRouter)
    this.app.use("/api/remark",remarkRouter)
    this.error()
  }
  private config() {
    this.app.use(cors())
    this.app.use(express.json())
    this.app.use(express.urlencoded({ extended: false }))
    this.app.use(session({
      secret:"zxh",
      cookie:{
        path:'/',
        httpOnly:true,
        maxAge:24*1000*60*60*30
      },
      store:sessionStore
    }))
  }
  private loginCh(){
    this.app.use(function(req,res,next){      
      loginCheck(req,res,next)
    })
  }
  private error() {
    this.app.use(function (req, res, next: NextFunction) {
      next(createError(404, "请求的资源不存在"));
    });
    // error handler
    this.app.use(function (err: Error, req: Request, res: Response, next: NextFunction) {

      res.send(err.message)
    });
  }
}
// module.exports = new App().app
export default new App().app
